﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Visits;
using System.ComponentModel;

    public class VisitListEventArgs : EventArgs
    {
        int _totalVisit = 0;
        public int TotalVisits { get { return _totalVisit; } set { } }

        public VisitListEventArgs(int totalVisit)
        {
            _totalVisit = totalVisit;
        }
    }

    public partial class VisitList : SharedUserControl 
    {
        string pre = string.Empty;
        int itemsPerRow = 0;
        int itemLimit = 0;
        protected string tdwidth;
        public string NoVisitsText { get; set; }
        public int ItemsPerRow { get { return itemsPerRow; } set { itemsPerRow = value; tdwidth = string.Format("{0}%", 100 / value); } }
        public bool ShowVisitTime { get; set; }
        public string VisitTimePrefix { get { return pre; } set { pre = value.Trim(); } }
        public int ItemLimit { get { return itemLimit; } set { itemLimit = value; } }
        //public event EventHandler<VisitListEventArgs> VisitListObtained;

        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ((Panel)e.Item.FindControl("pnVisitTime")).Visible = ShowVisitTime;
            Label prefix = (Label)e.Item.FindControl("lblPrefix");
            if (pre != string.Empty)
            {
                prefix.Visible = true;
                prefix.Text = pre;
            }
            else prefix.Visible = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Panel1.CssClass = CssClass;
            ListView1.GroupItemCount = itemsPerRow;
            List<Sc_SharedVisit> list = null;
            if (itemLimit > 0)
            {
                list = VisitOperations.GetVisitors(EntityType, ContextId, SortDirection.Descending, itemLimit);
            }
            else
            {
                list = VisitOperations.GetVisitors(EntityType, ContextId, SortDirection.Descending);
            }

            ListView1.DataSource = list;
            ListView1.DataBind();
        }

        protected void lblVisitDate_PreRender(object sender, EventArgs e)
        {
            Label lastVisitDate = (Label)sender;
            lastVisitDate.Text = Utility.FriendlyDateTime(lastVisitDate.Text, TimeDisplayEnum.LiteralHoursNoTimeInOtherDays, false);
        }
    }
}